﻿$(function () {
    $("#jqGrid").jqGrid({
        url: '../merchantinfo/list',
        datatype: "json",
        colModel: [		
            { label: '主键', name: 'merchantId', index: 'merchant_id', width: 60 ,key:true,hidden:true}, 	
			{ label: '申请人', name: 'username', index: 'm1.username', width: 60 }, 			
			{ label: '商铺名', name: 'shopName', index: 'm1.shopname', width: 100 }, 	
			/*{ label: '登录帐号', name: 'loginName', index: 's.username', width: 100 }, */	
			/*{ label: '住宅地址', name: 'address', index: 'm1.address', width: 100 }, */			
			{ label: '联系电话', name: 'mobilephone', index: 'm1.mobilephone', width: 90 }, 			
			{ label: '商铺固话', name: 'telephone', index: 'm1.shop_phone', width: 80 }, 			
			{ label: '商铺地址', name: 'shopAddress', index: 'm1.shop_address', width: 100 }, 	
			/*{ label: '父级商铺名称', name: 'parentShopName', index: 'parentShopName', width: 90 }, */
			{ label: '印鉴卡', name: 'sealCardImg' ,hidden:true}, 
			{ label: '二维码', name: 'payQRUrl', width: 60,sortable : false,
				formatter: function(value, options, row){
					var merNo = row.merchantNo;
					return '<a onclick="showInfo(' + JSON.stringify(merNo).replace(/"/g, '&quot;') + ')" target="view_window"><span class="label label-success" >预览</span></a>';
					/*if(value != null && value !=''){
						//return '<a href="../merchantinfo/image?id='+row.merchantNo+'" target="view_window"><span class="label label-success" >预览</span></a>';
						return '<a onclick="showInfo('+row.merchantNo+')" target="view_window"><span class="label label-success" >预览</span></a>';
					}else{
						return '';
					}*/
				} 	
			},
			{ label: '状态', name: 'status', index: 'm1.status', width: 60,
				formatter: function(value, options, row){
				var nub = row.presentNub;
                    if(value == 1){
                        return '<span class="label label-success">正常</span>';
                    }else if(value == 3){
						return '冻结';
					}
                    if(value != 1 && nub == 3){
						return '申请';
					}
					if(value != 1 && nub != 3){
						return '注册中';
					}
					else{
						return '';
					}
				}			
			}
        ],
		viewrecords: true,
        height: "100%",
        rowNum: 10,
		rowList : [10,20,30],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
});

function showInfo(id){
	parent.layer.open({
		  type: 2,
		  title: '二维码列表',
		  shadeClose: true,
		  shade: 0.8,
		  fixed: false, //不固定
		  maxmin: true,
		  area: ['60%', '80%'],
		  //content: 'merchant/listQRCode.html?id='+id //iframe的url
		  content: 'merchantinfo/getCodeList.do?id='+id //iframe的url
		}); 
}

var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
		21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
		33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",
		41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",
		46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",
		54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",
		65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			shopName: null
		},
		showList: true,
		title: null,
		merchantInfo: {},
		selected: '',
		cooperationoptions: [
	      { text: '收款', value: '1' },
	      { text: '吸粉', value: '2' }
	    ],
	    statusoptions:[
	    	{text:"申请",value:"0"},
	    	{text:"正常",value:"1"},
	    	{text:"注册中",value:"2"},
	    	{text:"挂失",value:"3"}
	    ],
	    iscreditCardoptions:[{text:"否",value:"0"},
	    	{text:"是",value:"1"}],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			$("#formDiv").show();
			$("#logoDiv").hide();
			$("#upDiv").hide();
			vm.showList = false;
			vm.title = "新增商户";
			vm.merchantInfo = {};
			
		},
		update: function (event) {
			
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			$("#formDiv").show();
			$("#logoDiv").hide();
			rowData = $('#jqGrid').jqGrid('getRowData',id);
			var status = rowData.status;
			var sealCardImg = rowData.sealCardImg;
			if(status == '申请' || status == '注册中'){
				$("#upDiv").show();
			}else{
				$("#upDiv").hide();
			}
			if(sealCardImg != null && sealCardImg != ''){
				$("input:radio[name='jiesuan']").eq(1).attr("checked",'checked');
				$('#settlementType2').show();
				$('#settlementType1').hide();
			}else{
				$("input:radio[name='jiesuan']").eq(0).attr("checked",'checked');
				$('#settlementType1').show();
				$('#settlementType2').hide();
			}
			
			vm.showList = false;
            vm.title = "修改商户";
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			var url = vm.merchantInfo.merchantId == null ? "../merchantinfo/save" : "../merchantinfo/update";
			$("#subButton").attr("disabled","disabled");
			if(vm.merchantInfo.parentMerchantId != null){
				url = "../merchantinfo/saveSonMerchant";
			}
			
			if(vm.merchantInfo.shopName == null || "" == vm.merchantInfo.shopNam){
				alert('企业名称不能为空');
				return;
			}
			
			if(vm.merchantInfo.shopAbbreviation == null || "" == vm.merchantInfo.shopAbbreviation){
				alert('企业简称不能为空');
				return;
			}
			
			if(vm.merchantInfo.shopAddress == null || "" == vm.merchantInfo.shopAddress){
				alert('企业地址不能为空');
				return;
			}
			
			
			var telPhone = vm.merchantInfo.telephone;
			if (!(/^0\d{2,3}-?\d{7,8}$/.test(telPhone))) {
	             alert("客服电话填写的格式不对，请正确填写");
	             return ;
			}
			
			var mobile = vm.merchantInfo.mobilephone;
			if (!(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|4|5|6|7|8|9][0-9]{9}))$/.test(mobile))) {
	             alert("联系人电话填写的格式不对，请正确填写");
	             return ;
			}
			
			var email = vm.merchantInfo.email;
			if (!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email))) {
	             alert("邮箱填写的不对正确，请正确填写");
	             return ;
			}
			
			var idCard = vm.merchantInfo.idCard;
			
			var iSum=0 ;
		    if(!/^\d{17}(\d|x)$/i.test(idCard)){
		    	alert("你输入的身份证长度或格式错误");
		    	return ; 
		    } 
		    idCard=idCard.replace(/x$/i,"a"); 
		    if(aCity[parseInt(idCard.substr(0,2))]==null){
		    	alert("你的身份证地区非法");
		    	return ; 
		    } 
		    sBirthday=idCard.substr(6,4)+"-"+Number(idCard.substr(10,2))+"-"+Number(idCard.substr(12,2)); 
		    var d=new Date(sBirthday.replace(/-/g,"/")) ;
		    if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
		    	alert("身份证上的出生日期非法");
		    	return ; 
		    }
		    for(var i = 17;i>=0;i --){
		    	iSum += (Math.pow(2,i) % 11) * parseInt(idCard.charAt(17 - i),11) ;
		    }
		    if(iSum%11!=1){
		    	alert("你输入的身份证号非法");
		    	return ; 
		    } 
			
			
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.merchantInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
			    	$("#subButton").removeAttr("disabled");
				}
			});
		},
		upLogo: function (event) {//上传logo
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			$("#formDiv").hide();
			$("#logoDiv").show();
            vm.title = "上传logo";
            vm.getInfo(id);
		},
		addSonMerchant: function(){//新增子商户
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			rowData = $('#jqGrid').jqGrid('getRowData',id);
			var parentMerchantId = rowData.merchantId;
			var parentShopName = rowData.parentShopName;
			if(parentShopName != null && parentShopName != '' ){
				//parentShopName.length > 0
				alert('该商户已是二级商户，无法继续新增子商户');
				return ;
			}
			$("#formDiv").show();
			$("#logoDiv").hide(); 
			$("#upDiv").hide();
			vm.showList = false;
			vm.title = "新增子商户";
			vm.merchantInfo = {
					parentMerchantId : parentMerchantId
			};
			
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../merchantinfo/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		review: function (event) {//审核商户
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			//var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
			confirm('确定要审核选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../merchantinfo/review",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		//冻结
		frozen: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			for(var i = 0;i<ids.length;i++){
				rowData = $('#jqGrid').jqGrid('getRowData',ids[i]);
				var status = rowData.status;
				if(status != '<span class="label label-success">正常</span>'){
					alert('所选数据状态非正常状态');
					return;
				}
			}
			confirm('确定要冻结选中的企业吗？', function(){
				$.ajax({
					type: "POST",
				    url: "../merchantinfo/frozen",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert('数据异常...');
						}
					}
				});
			});
		},
		//解冻
		unFrozen: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			for(var i = 0;i<ids.length;i++){
				rowData = $('#jqGrid').jqGrid('getRowData',ids[i]);
				var status = rowData.status;
				if(status != '冻结'){
					alert('所选数据状态非冻结状态');
					return;
				}
			}
			confirm('确定要解冻选中的企业吗？', function(){
				$.ajax({
					type: "POST",
				    url: "../merchantinfo/unFrozen",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert('数据异常...');
						}
					}
				});
			});
		},
		dataDoZip: function (event) {//审核商户
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			window.location.href="../merchantinfo/dataDoZip?id="+id;
			/*confirm('确定将进件资料打包成zip下载吗？', function(){
				$.ajax({
					type: "POST",
				    url: "../merchantinfo/dataDoZip",
				    data: JSON.stringify(id),
				    success: function(r){
				    	console.log()
						if(r.code == 0){
							alert(r.msg, function(index){
								//$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
				    	alert(msg)
					}
				});
			});*/
		},
		createQRcode: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			$.ajax({
				type: "POST",
			    url: "../merchantinfo/createQRcode",
			    data: JSON.stringify(ids),
			    success: function(r){
					if(r.code == 0){
						alert('操作成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(id){
			$.get("../merchantinfo/info/"+id, function(r){
                vm.merchantInfo = r.merchantInfo;
            });
		},
		view: function(id){
			
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			page = 1;
			var keyword = $("#keyword").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{keyword:keyword},
            }).trigger("reloadGrid");
		}
	}
});